
import 'package:flutter_boost/flutter_boost.dart';
import 'package:t3_data_service/biz_data_source/map/address/city_pick/model/host_model.dart';
import 'package:t3_data_service/biz_data_source/map/address/poi_search/poi_search.dart';
import 'package:t3_foundation/t3_foundation.dart';
import 'package:uikit/uikit.dart';

class MapSelectedHeaderViewModel extends ViewModel {
  var city = HostModel().obs;
  /// 仅用于标题更新
  var currentPOI = POIModel(location: const LatLng(0, 0)).obs;

  updateCity(HostModel hostModel) {
    cityChangeNotify(hostModel);
    city.value = hostModel;
  }

  cityChangeNotify(HostModel model) {
    if (model.city != null && city.value.city != null) {
      if (model.city != city.value.city) {
        Map<String, Object> pa = {
          "city":model.city as Object,
          "adCode":model.adCode as Object,
        };
        BoostChannel.instance.sendEventToNative('cityDidChanged', pa);
      }
    }
  }

  updatePOI(POIModel? poi) {
    if (poi != null) {
      currentPOI.value = poi;

      var city = poi.city;
      var adCode = poi.adCode;
      if (city.isNotNullAndEmpty() && adCode.isNotNullAndEmpty() && LatLng.isLatLngValid(poi.location)) {
        var host = HostModel(city: city, adCode: adCode, latitude: poi.location.latitude, longitude: poi.location.longitude);
        updateCity(host);
      }
    }
  }
}